﻿
using Data.Model.MySql;

using DataService.Cache.Data;
using DataService.Core.DataHelper;
using DataService.Core.ServiceDTO;
using DataService.Data;
using DataService.Main;
using MySql.Data.MySqlClient;
using Org.BouncyCastle.Utilities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UnitTest
{
    public class TestMySql
    {
          private string ConnStr = "Server=主机;port=端口;;Database=数据库名;Uid=root;Pwd=密码;Charset=utf8";
        public void CreateModel()
        {
            using (var db = DataService.DBMangerFactory.CreateProvider(DatabaseType.MySql))
            {
                db.ConnectionString = ConnStr;

                ModelDAL.CreateModelCodes(@"D:\DataService\UnitTest\Model\", db, "TestIndexTable");

            }
            Console.WriteLine("完成");
        }
        public void CreateTable()
        {

            using (var db = DataService.DBMangerFactory.CreateProvider(DatabaseType.MySql))
            {
                db.ConnectionString = ConnStr;
                db.TableSuffix = "";
                ModelDAL.CreatTableSchema("PersonnelInfo", db);
                ModelDAL.CreateModelCodes(@"D:\DataService\UnitTest\Model\", db, "PersonnelInfo", "testindextable");
            }
            Console.WriteLine("Mysql自动建表完成");
        }

        public void Insert_1()
        {
            Snowflake snowflake1 = new Snowflake();
            using (var db = DataService.DBMangerFactory.CreateProvider(DatabaseType.MySql))
            {
                db.IsEnable = true;
                 study_test study_Test = new study_test();
                study_Test.Id = snowflake1.NewId();
                study_Test.UserName = "分表分库插入，再次雪花插入";
                study_Test.Age = 18;
                study_Test.Sex = true;
                study_Test.StudyTime = DateTime.Now;
                study_Test.TestPrice = 9623;

                db.ConnectionString = ConnStr;
                db.TableSuffix = "";
                DAL.Insert<study_test>(study_Test,db);
              //  ModelDAL.CreateModelCodes(@"D:\资料相关\框架代码\DataService\UnitTest\Model\MySql\", db, "study_test");
            }
            Console.WriteLine("完成");
        }

        public void TestInsert()
        {
            //  byte[] data = Encoding.Unicode.GetBytes("FAC6A0C0-BBA6-4AA5-A652-EB321BD5DAF8");
            byte[] data = Encoding.Unicode.GetBytes("FAC6");
            Console.WriteLine(data.Length);
            SnowflakeCore snowflake1 = new SnowflakeCore();
            var conn = new MySqlConnection(ConnStr);
            conn.Open();

            string createDBCommand = "INSERT INTO testindextable(Id,sfzUniqueIndex_1,sfzUniqueIndex_2,nameIndex_1,nameIndex_2,lh_1,lh_2,c1,t) VALUES(@Id,@sfzUniqueIndex_1,@sfzUniqueIndex_2,@nameIndex_1,@nameIndex_2,@lh_1,@lh_2,@c1,@t)";

            MySqlCommand cmd = new MySqlCommand(createDBCommand, conn);
            var tran = conn.BeginTransaction(IsolationLevel.RepeatableRead);
            cmd.Transaction = tran;

            cmd.Parameters.AddWithValue("@Id", snowflake1.NewId());
            cmd.Parameters.AddWithValue("@sfzUniqueIndex_1", "测试1");
            cmd.Parameters.AddWithValue("@sfzUniqueIndex_2", "测试3");
            cmd.Parameters.AddWithValue("@nameIndex_1", "测试4");
            cmd.Parameters.AddWithValue("@nameIndex_2", "测试5");
            cmd.Parameters.AddWithValue("@lh_1", "测试6");
            cmd.Parameters.AddWithValue("@lh_2", "测试7");
            cmd.Parameters.AddWithValue("@c1", data);
            cmd.Parameters.AddWithValue("@t", DateTime.UtcNow);
            cmd.ExecuteNonQuery();


            foreach (IDbDataParameter item in cmd.Parameters)
            {
                string key = item.ParameterName;
                object value = item.Value;
            }
            tran.Commit();
            cmd.Dispose();
            conn.Dispose();


        }
        #region 随机姓名
        public List<string> Xing = new List<string>() {"赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "楮", "卫", "蒋", "沈", "韩", "杨",
        "朱", "秦", "尤", "许", "何", "吕", "施", "张", "孔", "曹", "严", "华", "金", "魏", "陶", "姜",
        "戚", "谢", "邹", "喻", "柏", "水", "窦", "章", "云", "苏", "潘", "葛", "奚", "范", "彭", "郎",
        "鲁", "韦", "昌", "马", "苗", "凤", "花", "方", "俞", "任", "袁", "柳", "酆", "鲍", "史", "唐",
        "费", "廉", "岑", "薛", "雷", "贺", "倪", "汤", "滕", "殷", "罗", "毕", "郝", "邬", "安", "常",
        "乐", "于", "时", "傅", "皮", "卞", "齐", "康", "伍", "余", "元", "卜", "顾", "孟", "平", "黄",
        "和", "穆", "萧", "尹", "姚", "邵", "湛", "汪", "祁", "毛", "禹", "狄", "米", "贝", "明", "臧",
        "计", "伏", "成", "戴", "谈", "宋", "茅", "庞", "熊", "纪", "舒", "屈", "项", "祝", "董", "梁",
        "杜", "阮", "蓝", "闽", "席", "季", "麻", "强", "贾", "路", "娄", "危", "江", "童", "颜", "郭",
        "梅", "盛", "林", "刁", "锺", "徐", "丘", "骆", "高", "夏", "蔡", "田", "樊", "胡", "凌", "霍",
        "虞", "万", "支", "柯", "昝", "管", "卢", "莫", "经", "房", "裘", "缪", "干", "解", "应", "宗",
        "丁", "宣", "贲", "邓", "郁", "单", "杭", "洪", "包", "诸", "左", "石", "崔", "吉", "钮", "龚",
        "程", "嵇", "邢", "滑", "裴", "陆", "荣", "翁", "荀", "羊", "於", "惠", "甄", "麹", "家", "封",
        "芮", "羿", "储", "靳", "汲", "邴", "糜", "松", "井", "段", "富", "巫", "乌", "焦", "巴", "弓",
        "牧", "隗", "山", "谷", "车", "侯", "宓", "蓬", "全", "郗", "班", "仰", "秋", "仲", "伊", "宫",
        "宁", "仇", "栾", "暴", "甘", "斜", "厉", "戎", "祖", "武", "符", "刘", "景", "詹", "束", "龙",
        "叶", "幸", "司", "韶", "郜", "黎", "蓟", "薄", "印", "宿", "白", "怀", "蒲", "邰", "从", "鄂",
        "索", "咸", "籍", "赖", "卓", "蔺", "屠", "蒙", "池", "乔", "阴", "郁", "胥", "能", "苍", "双",
        "闻", "莘", "党", "翟", "谭", "贡", "劳", "逄", "姬", "申", "扶", "堵", "冉", "宰", "郦", "雍",
        "郤", "璩", "桑", "桂", "濮", "牛", "寿", "通", "边", "扈", "燕", "冀", "郏", "浦", "尚", "农",
        "温", "别", "庄", "晏", "柴", "瞿", "阎", "充", "慕", "连", "茹", "习", "宦", "艾", "鱼", "容",
        "向", "古", "易", "慎", "戈", "廖", "庾", "终", "暨", "居", "衡", "步", "都", "耿", "满", "弘",
        "匡", "国", "文", "寇", "广", "禄", "阙", "东", "欧", "殳", "沃", "利", "蔚", "越", "夔", "隆",
        "师", "巩", "厍", "聂", "晁", "勾", "敖", "融", "冷", "訾", "辛", "阚", "那", "简", "饶", "空",
        "曾", "毋", "沙", "乜", "养", "鞠", "须", "丰", "巢", "关", "蒯", "相", "查", "后", "荆", "红",
        "游", "竺", "权", "逑", "盖", "益", "桓", "公", "仉", "督", "晋", "楚", "阎", "法", "汝", "鄢",
        "涂", "钦", "岳", "帅", "缑", "亢", "况", "后", "有", "琴", "归", "海", "墨", "哈", "谯", "笪",
        "年", "爱", "阳", "佟", "商", "牟", "佘", "佴", "伯", "赏",
        "万俟", "司马", "上官", "欧阳", "夏侯", "诸葛", "闻人", "东方", "赫连", "皇甫", "尉迟", "公羊",
        "澹台", "公冶", "宗政", "濮阳", "淳于", "单于", "太叔", "申屠", "公孙", "仲孙", "轩辕", "令狐",
        "锺离", "宇文", "长孙", "慕容", "鲜于", "闾丘", "司徒", "司空", "丌官", "司寇", "子车", "微生",
        "颛孙", "端木", "巫马", "公西", "漆雕", "乐正", "壤驷", "公良", "拓拔", "夹谷", "宰父", "谷梁",
        "段干", "百里", "东郭", "南门", "呼延", "羊舌", "梁丘", "左丘", "东门", "西门", "南宫"};

        string _lastNameMan = "刚伟勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄琛钧冠策腾楠榕风航弘";


        string _lastNameWoMan = "秀娟英华慧巧美娜静淑惠珠翠雅芝玉萍红娥玲芬芳燕彩春菊兰凤洁梅琳素云莲真环雪荣爱妹霞香月莺媛艳瑞凡佳嘉琼勤珍贞莉桂娣叶璧璐娅琦晶妍茜秋珊莎锦黛青倩婷姣婉娴瑾颖露瑶怡婵雁蓓纨仪荷丹蓉眉君琴蕊薇菁梦岚苑婕馨瑗琰韵融园艺咏卿聪澜纯毓悦昭冰爽琬茗羽希宁欣飘育滢馥筠柔竹霭凝鱼晓欢霄枫芸菲寒伊亚宜可姬舒影荔枝思丽墨";

        #endregion
        #region 随机生成专业
        string[] majors = new string[] { "通信与计算机网络技术", "网络工程", "网络经济概论", "建筑学", "国际经济学", "市场营销", "会计学", "水利工程", "经管法", "生物工程" };
        #endregion
        #region 随机电话
        string[] phones = new string[] { "13", "15", "16", "17", "18", "19" };


        #endregion

        public void Insert()
        {
            byte[] image = Encoding.UTF8.GetBytes("啊手动阀即使对方");


            using (var db = DataService.DBMangerFactory.CreateProvider(DatabaseType.MySql))
            {
                string connectionString = ConnStr;
                db.ConnectionString = connectionString;

                SnowflakeCore snowflake1 = new SnowflakeCore();

                int XingLen = Xing.Count;//501个姓
                char[] ManChar = _lastNameMan.ToCharArray();//151的男士名
                char[] WoManChar = _lastNameWoMan.ToCharArray();//151个女士名
                int ManNameLen = ManChar.Length;//151的男士名
                int WoManNameLen = WoManChar.Length;//151个女士名
                int tempNo = 0;
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                for (int n = 0; n <1; n++)
                {
                    Guid departmentId = Guid.NewGuid();
                    List<PersonnelInfo> list = new List<PersonnelInfo>();

                    Random random = new Random();
                    int xingRand = random.Next(XingLen);
                    int mingRand = random.Next(ManNameLen);
                    string xm = Xing[xingRand] + ManChar[mingRand];
                    //  string xm = "十发达斯洛伐克";
                    for (int i = 0; i < 30; i++)
                    {
                        tempNo = tempNo + 1;
                        string personNo = tempNo.ToString().PadLeft(6, '0');
                        string idNo = "000000000000000000";
                        try
                        {
                            idNo = IDCardNumber.generate();
                        }
                        catch
                        {
                            idNo = "000000000000000000";
                        }
                        Random random1 = new Random();
                        int majorRand = random1.Next(majors.Length);
                        string tempfullMajor = majors[majorRand];
                        majorRand = random1.Next(majors.Length);
                        string partMajor = majors[majorRand];

                        PersonnelInfo testModel = new PersonnelInfo();
                        testModel.ID = snowflake1.NewId(DateTime.Now.AddMonths(-1));
                        testModel.PersonnelName = xm;
                        testModel.PersonnelNo = personNo;
                        testModel.Status = 1;
                        testModel.FullTimeMajor = tempfullMajor;
                        testModel.PartTimeMajor = partMajor;
                        testModel.IDNo = "xxxxxxxxxxxxxxxxxxx";
                        //testModel.Sex = false;
                        testModel.TermOfValidity = DateTime.Now.ToShortDateString();
                        testModel.LearningWay = "全日制";
                        testModel.WorkYear = RandHelper.Next(2, 60);
                        int num = random.Next(7);
                        string tempStr1 = string.Empty;
                        for (int l = 0; l < num; l++)
                        {

                            for (int k = 0; k < 7; k++)
                            {
                                int tempRand = random.Next(ManNameLen);
                                tempStr1 = tempStr1 + ManChar[tempRand];
                            }

                        }

                        testModel.FullTimeUniversity = tempStr1;


                        int num1 = random.Next(7);
                        string tempStr2 = string.Empty;
                        for (int l = 0; l < num1; l++)
                        {

                            for (int k = 0; k < 6; k++)
                            {
                                int tempRand = random.Next(ManNameLen);
                                tempStr2 = tempStr2 + ManChar[tempRand];
                            }

                        }
                        testModel.PartTimeUniversity = tempStr2;
                        int d = RandHelper.Next(111111111, 999999999);

                        int pd = random1.Next(phones.Length);
                        string s = phones[pd];
                        s = s + d.ToString();

                        testModel.Phone = s;

                        int dd = random1.Next(100);
                        testModel.FullTimeGraduationTime = DateTime.Now.AddDays(dd).ToString("yyyy-MM-dd");
                        dd = random1.Next(100);
                        testModel.PartTimeGraduationTime = DateTime.Now.AddDays(dd).ToString("yyyy-MM-dd");
                        dd = random1.Next(100);
                        testModel.SocialLastTime = DateTime.Now.AddDays(dd).ToString("yyyy-MM-dd");
                        testModel.EmployeeType = "在职";

                        int dds = random.Next(6);
                        List<string> tempDatas = new List<string>();
                        for (int l = 0; l < dds; l++)
                        {
                            string tempStr = string.Empty;
                            for (int k = 0; k < 5; k++)
                            {
                                int tempRand = random.Next(ManNameLen);
                                tempStr = tempStr + ManChar[tempRand];
                            }
                            tempDatas.Add(tempStr);
                        }

                        testModel.CertificateNames = string.Join(",", tempDatas);
                        testModel.DepartmentId = departmentId.ToString();

                        testModel.TestC1 = image;

                        list.Add(testModel);
                    }

                    DAL.Insert(list, db);
                    db.Commit();

                }

                stopwatch.Stop();

                Console.WriteLine($"完成，耗时：{stopwatch.ElapsedMilliseconds}");

            }

        }



        public void ReadData()
        {



            using (var db = DataService.DBMangerFactory.CreateProvider(DatabaseType.MySql))
            {
                string connectionString = ConnStr;
                db.ConnectionString = connectionString;
                int totalCount = 0;
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                //页码，先正向查询，再逆向，再正向
                int[] pageIndexs = new int[] { 1, 2, 6, 8, 2, 3, 7, 1, 5 };
                ResultPageDTO resultPageDTO = new ResultPageDTO();
                foreach (var itemIndex in pageIndexs)
                {

                    RequestPageDTO paramDTO = new RequestPageDTO
                    {
                        PageIndex = itemIndex,
                        PageSize = 20,
                        ParamNextStartId = resultPageDTO.ParamNextStartId,
                        ParamUpStartId = resultPageDTO.ParamUpStartId,
                        UniqueKey = resultPageDTO.UniqueKey
                    };
                    //  db.TableSuffix = "_20221127";
                    // var tempArray = DAL.QueryPage<TB_PersonnelInformation>(x =>true&&x.PersonnelName.Contains("第1000批次"),30, 499999, null, null, out recordCount, db,t=>new {t.ID,t.PersonnelNo,t.PersonnelName });

                    //&& x.PersonnelName== "劳光"
                    string[] ffff = new string[] { "测试1", "测试2", "测试3" };
                    //x => true&& x.DepartmentId!= ""&&x.PersonnelNo=="ddd"&&x.PersonnelName=="aaa"&&x.IsDelete
                    var tempArray = DAL.QueryPage<Data.Model.MySql.PersonnelInfo>(x => x.PersonnelName == "归宏", paramDTO, db, ref resultPageDTO, t => new { t.ID, t.PersonnelNo });
                    Console.WriteLine($"第{itemIndex}页返回的唯一标识：{resultPageDTO.UniqueKey}，最大Id：{resultPageDTO.ParamNextStartId},最小Id{resultPageDTO.ParamUpStartId}");
                    Console.WriteLine($"****************************第{itemIndex}页*******************************");
                    var tempList = tempArray.ToList();
                    foreach (var item in tempList)
                    {
                        Console.WriteLine(item.ID + "**" + item.PersonnelName + "***" + item.PersonnelNo + "**" + item.AuditState);
                    }
                    Console.WriteLine($"****************************第{itemIndex}页*******************************");
                    totalCount = resultPageDTO.TotalCount;
                }
                stopwatch.Stop();
                Console.WriteLine($"读取数据流耗时：{stopwatch.ElapsedMilliseconds}；总行数：{totalCount}");
                stopwatch.Restart();

                stopwatch.Stop();



                Console.WriteLine("完成");


            }

        }

    }
}
